package com.rjj.wlqq.wxpay.wxpay.wxconfig;

import com.rjj.wlqq.wxpay.wxpay.entity.Wx;
import com.rjj.wlqq.wxpay.wxsdk.IWXPayDomain;
import com.rjj.wlqq.wxpay.wxsdk.WXPayConfig;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author r
 * @date 2021/1/19 10:46
 */
@Slf4j
public class WXPayConfigImpl extends WXPayConfig
{

    private Wx wx;

    public WXPayConfigImpl(Wx wx)
    {
        this.wx = wx;
    }

    @Override
    public String getAppID()
    {
        return wx.getAppId();
    }

    @Override
    public String getMchID()
    {
        return wx.getMchId();
    }

    /**
     * 私钥
     */
    @Override
    public String getKey()
    {
        return wx.getKey();
    }

    /**
     * 支付证书路径
     */
    @Override
    public InputStream getCertStream()
    {
        final File file = new File(wx.getPathFile());
        try (FileInputStream inputStream = new FileInputStream(file))
        {
            return inputStream;
        } catch (IOException e)
        {
            log.error("读取证书失败：{}", e);
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public boolean shouldAutoReport()
    {
        //return super.shouldAutoReport();
        return false;
    }


    @Override
    public IWXPayDomain getWXPayDomain()
    {
        return new IWXPayDomain()
        {
            @Override
            public void report(String domain, long elapsedTimeMillis, Exception ex)
            {
                System.out.println("域名 " + domain + " 问题：" + ex);
            }

            @Override
            public DomainInfo getDomain(WXPayConfig config)
            {
                return new DomainInfo("api.mch.weixin.qq.com", true);
            }
        };
    }
}
